package com.eightbears.beboy.ui.merchant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.ShopManageAdapter;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.InputMethodUtils;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;
import com.eightbears.beboy.base.view.qrcode.utils.CodeUtils;
import com.eightbears.beboy.base.view.qrcode.view.CaptureActivity;
import com.eightbears.beboy.entity.ShopManageEntity;
import com.eightbears.beboy.ui.user.activity.ShopInfoActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_ACCOUNT;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_AUTHOR;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_COMING_SOON;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_DATA;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_GET_QC;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_INFO;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_JISHI;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_RESERVE;
import static com.eightbears.beboy.entity.ShopManageEntity.MENU_SHOP_SET_MEAL;

public class ShopManageActivity extends BaseActivity {

    public static final int REQUEST_CODE = 0x01;
    public static final String PARAMS_VALID_CODE = "validCode";

    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.et_qc_code)
    EditText et_qc_code;
    @BindView(R.id.tv_shop_ensure)
    TextView tv_shop_ensure;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private ShopManageAdapter shopManageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_manage);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initAdapter();
    }

    @Override
    protected void initData() {
        tv_title.setText(SPUtil.getUser().getData().getShops().get(0).getTitle());
    }

    @Override
    protected void initEvent() {
        shopManageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ShopManageEntity manageEntity = (ShopManageEntity) adapter.getData().get(position);
                switch (manageEntity.getType()) {
                    case MENU_SHOP_INFO:
                        startLocalActivity(ShopManageActivity.this, ShopInfoActivity.class);
                        break;
                    case MENU_SHOP_ACCOUNT:
                        startLocalActivity(ShopManageActivity.this, ShopAccountActivity.class);
                        break;
                    case MENU_SHOP_AUTHOR:
                            startLocalActivity(ShopManageActivity.this, ShopPowerManageActivity.class);
                        break;
                    case MENU_SHOP_SET_MEAL:
                        startLocalActivity(ShopManageActivity.this, ShopSetMealActivity.class);
                        break;
                    case MENU_SHOP_RESERVE:
                        startLocalActivity(ShopManageActivity.this, ShopReservesActivity.class);
                        break;
                    case MENU_SHOP_DATA:
                        startLocalActivity(ShopManageActivity.this, ShopDataValActivity.class);
                        break;
                    case MENU_SHOP_JISHI:
                        startLocalActivity(ShopManageActivity.this, TechnicianManageActivity.class);
                        break;
                    case MENU_SHOP_GET_QC:
                        startLocalActivity(ShopManageActivity.this, QCCodePayActivity.class);
                        break;
                    case MENU_SHOP_COMING_SOON:
                        break;
                }
            }
        });

        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    private void initAdapter() {
        rv_list.setLayoutManager(new MyLinearLayoutManager(this));
        shopManageAdapter = new ShopManageAdapter();
        rv_list.setAdapter(shopManageAdapter);
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    @OnClick(R.id.tv_shop_ensure)
    void tv_shop_ensure() {
        String code = et_qc_code.getText().toString().trim();
        if (!TextUtils.isEmpty(code)) {
            startLocalActivity(ShopManageActivity.this, ShopValidCodeActivity.class, PARAMS_VALID_CODE, code);
        } else {
            ShowToast.showShortToast(getString(R.string.alert_input_empty));
        }
    }

    @OnClick(R.id.ll_qc_code)
    void ll_qc_code() {
        startActivityForResult(new Intent(this, CaptureActivity.class), REQUEST_CODE);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @OnClick(R.id.ll_valid_data)
    void ll_valid_data() {
        startLocalActivity(this, ValidRecordActivity.class);
    }

    @Override
    public void finish() {
        super.finish();
        InputMethodUtils.closeInputMethod(this, et_qc_code);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (data != null && data.getExtras() != null) {

                Bundle bundle = data.getExtras();
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String qcCode = bundle.getString(CodeUtils.RESULT_STRING);
                    if(qcCode.contains("beboy://code/")) {
                        qcCode = qcCode.substring(qcCode.lastIndexOf("/") + 1, qcCode.length());
                    }
                    startLocalActivity(ShopManageActivity.this, ShopValidCodeActivity.class, PARAMS_VALID_CODE, qcCode);
                } else {
                    ShowToast.showShortToast(getString(R.string.alert_qc_valid_fail));
                }
            }
        }
    }
}
